@echo off
setlocal enabledelayedexpansion
pushd %~dp0

for %%i in (bin lib build) do (
    if exist %%i\* del /q %%i\*
    if not exist %%i mkdir %%i
)
del /q *.pdb

echo MZ............................................................> build\t

set LUAJIT_ROOT=luajit
set SRC=%LUAJIT_ROOT%\src
set DYNASM=%LUAJIT_ROOT%\dynasm
set LJ_DEFS=
set LJ_DEFS=%LJ_DEFS% /D _CRT_SECURE_NO_WARNINGS
set LJ_DEFS=%LJ_DEFS% /D _CRT_SECURE_NO_DEPRECATE
set LJ_DEFS=%LJ_DEFS% /D LUAJIT_ENABLE_CHECKHOOK
set LJ_DEFS=%LJ_DEFS% /D LUAJIT_ENABLE_JIT
set LJ_DEFS=%LJ_DEFS% /D LUAJIT_ENABLE_TABLE_BUMP
set LJ_DEFS=%LJ_DEFS% /D LUAJIT_ENABLE_LUA52COMPAT
set LJ_DEFS=%LJ_DEFS% /D LUAJIT_USE_SYSMALLOC

set LJCOMP_DEBUG=/O1
set LJLINK_DEBUG=
set LJML_DEBUG=

if "%1"x equ "debug"x (
    set LJ_DEFS=%LJ_DEFS% /D LUA_USE_ASSERT
    set LJ_DEFS=%LJ_DEFS% /D LUA_USE_APICHECK
    set LJCOMP_DEBUG=/Zi
    set LJLINK_DEBUG=/Debug
    set LJML_DEBUG=/Zi
)

set ALL_LIB=
set ALL_LIB=%ALL_LIB% %SRC%\lib_base.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_math.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_bit.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_string.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_table.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_io.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_os.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_package.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_debug.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_jit.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_ffi.c
set ALL_LIB=%ALL_LIB% %SRC%\lib_buffer.c

set LJCOMPILE=cl /nologo /c /W3 /I %SRC% /I build /I %DYNASM% /Fobuild\
set LJCOMPILE=%LJCOMPILE% /GS- /GR- /Gy- /Oi- %LJCOMP_DEBUG%
set LJLINK=link /nologo /incremental:no /safeseh:no /merge:.rdata=.text /emitpogophaseinfo /stub:build\t %LJLINK_DEBUG%

%LJCOMPILE% %LJ_DEFS% %SRC%\host\minilua.c || goto BAD
%LJLINK% /out:build\minilua.exe build\minilua.obj || goto BAD

set LJ_DEFS=%LJ_DEFS% /D _NO_CRT_STDIO_INLINE

set ARCH=x64
set SUBSYSTEM=console
set LJML=ml64
set DASMFLAGS=-D WIN -D JIT -D FFI -D P64
build\minilua.exe
if errorlevel 8 goto :X64

set ARCH=x86
set SUBSYSTEM=console,5.01
set LJML=ml
set LJ_DEFS=%LJ_DEFS% /D _USE_32BIT_TIME_T
set DASMFLAGS=-D WIN -D JIT -D FFI
set LJCOMPILE=%LJCOMPILE% /arch:IA32

:X64
set LJCOMPILE=%LJCOMPILE% %LJ_DEFS%
set LJLIB=lib /nologo /nodefaultlib /machine:%ARCH%
set LJMT=mt /nologo
set LJML=%LJML% /nologo /c /Gy- %LJML_DEBUG% /W3 /Fobuild\

:: for %%i in (defs\*.def) do (
::     copy %%i build\%%~nxi
::     if exist defs\%ARCH%\%%~nxi type defs\%ARCH%\%%~nxi >> build\%%~nxi
::     %LJLIB% /def:build\%%~nxi /out:build\%%~ni.lib || goto BAD
:: )

set SYSLIB=kernel32.lib
for %%i in (defs\%ARCH%\*.def) do (
    %LJLIB% /def:%%i /out:build\%%~ni.lib || goto BAD
    set SYSLIB=!SYSLIB! build\%%~ni.lib
)
%LJCOMPILE% crt\crt.c || goto BAD
set SYSLIB=!SYSLIB! build\crt.obj
if exist crt\msvc_%ARCH% (
    %LJML% crt\msvc_%ARCH%\crtasm.asm || goto BAD
    set SYSLIB=!SYSLIB! build\crtasm.obj
)

set LJLINK=%LJLINK% /subsystem:%SUBSYSTEM% /nodefaultlib %SYSLIB%

build\minilua %DYNASM%\dynasm.lua -LN %DASMFLAGS% -o build\buildvm_arch.h %SRC%\vm_%ARCH%.dasc || goto BAD
%LJCOMPILE% %SRC%\host\buildvm*.c || goto BAD
%LJLINK% /out:build\buildvm.exe build\buildvm*.obj || goto BAD

build\buildvm -m peobj -o build\lj_vm.obj || goto BAD
build\buildvm -m bcdef -o build\lj_bcdef.h %ALL_LIB% || goto BAD
build\buildvm -m ffdef -o build\lj_ffdef.h %ALL_LIB% || goto BAD
build\buildvm -m libdef -o build\lj_libdef.h %ALL_LIB% || goto BAD
build\buildvm -m recdef -o build\lj_recdef.h %ALL_LIB% || goto BAD
build\buildvm -m vmdef -o build\vmdef.lua %ALL_LIB% || goto BAD
build\buildvm -m folddef -o build\lj_folddef.h %SRC%\lj_opt_fold.c|| goto BAD

%LJCOMPILE% %SRC%\lj_*.c %SRC%\lib_*.c || goto BAD
%LJLIB% /out:lib\lua51.lib build\lj_*.obj build\lib_*.obj || goto BAD
%LJCOMPILE% %SRC%\luajit.c || goto BAD
%LJLINK% /release /out:bin\luajit.exe build\luajit.obj lib\lua51.lib || goto BAD

rmdir /s /q build

goto END
:BAD
endlocal
echo !!!ERROR!!!
:END
popd
